import java.util.*;
import java.util.concurrent.*;

class MyThreadPool{
    //用阻塞队列来存放任务
    private BlockingDeque<Runnable> queue = new LinkedBlockingDeque<>();

    public void summit(Runnable runnable) throws InterruptedException{
        queue.put(runnable);
    }

    public MyThreadPool(int n){
        for (int i = 0; i < n; i++) {
            Thread t1 = new Thread(() -> {
                while(true){
                    try {
                        Runnable runnable = queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t1.start();
            System.out.println("创建线程 "+ i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }



}
public class ThreadDemo1 {
    public static void main(String[] args) throws InterruptedException{
        MyThreadPool pool = new MyThreadPool(10);
        Thread.sleep(12000);
        for (int i = 0; i < 1000; i++) {
            int number = i;
            pool.summit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello " + number);
                }
            });
        }

    }
}
